﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.DAL;
using System.Data.SqlClient;
using System.Data;


namespace PBOffice.BLL
{
   public class MeetingManagerBLL
    {

        #region 查询会议室信息
        /// <summary>
        /// 查询会议室信息
        /// </summary>
        /// <returns></returns>
        public List<v_MeetingRoomType> ShowMeetingInfor()
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var infor = from c in myoffice.v_MeetingRoomType
                            select c;
                return infor.ToList();
            }
        } 
        #endregion


        #region 根据会议室类型查询会议室信息
        /// <summary>
        /// 查询会议室信息
        /// </summary>
        /// <returns></returns>
        public List<v_MeetingRoomType> GetMeeting(string  roomTypeId)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var infor = from c in myoffice.v_MeetingRoomType
                            where c.RoomTypeId==roomTypeId
                            select c;
                return infor.ToList();
            }
        }
        #endregion


        #region 根据会议室编号查询会议室信息
        /// <summary>
        /// 根据会议室编号查询会议室信息
        /// </summary>
        /// <param name="roomNo"></param>
        /// <returns></returns>
        public List<MeetingRoom> GetRoomByNo(int roomNo)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var num = from c in myoffice.MeetingRooms
                          where c.RoomNo == roomNo
                          select c;
                return num.ToList();
            }

        } 
        #endregion


        #region 添加会议室信息

        /// <summary>
        /// 添加会议室信息
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>

        public bool AddMeetingInfor(MeetingRoom room)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                MeetingRoom roomInfor = new MeetingRoom()
                {
                    RoomId = room.RoomId,//会议室名字
                    RoomArea = room.RoomArea,//会议室面积
                    RoomPeopleNo = room.RoomPeopleNo,//会议室人数
                    RoomEquitpment = room.RoomEquitpment,//会议室装备
                    RoomImage = room.RoomImage,//会议室图片
                   
                };
                
                roomInfor.RoomType =myoffice.RoomTypes.SingleOrDefault(r=>r.RoomTypeId== room.RoomType.RoomTypeId);//会议室类型
                myoffice.MeetingRooms.AddObject(roomInfor);
                return myoffice.SaveChanges() > 0;//如果添加成功，返回true。否则添加失败，返回false
            }
        } 
        #endregion


        #region 更新会议室信息
        /// <summary>
        /// 更新会议室信息
        /// </summary>
        /// <param name="setRoom"></param>
        /// <returns></returns>
        public bool UpdateMeetingInfor(MeetingRoom setRoom)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var rom = from c in myoffice.MeetingRooms
                          where c.RoomNo == setRoom.RoomNo
                          select c;
                foreach (var item in rom)
                {
                    item.RoomId = setRoom.RoomId;
                    item.RoomPeopleNo = setRoom.RoomPeopleNo;
                    item.RoomEquitpment = setRoom.RoomEquitpment;
                    item.RoomArea = setRoom.RoomArea;
                    item.RoomImage = setRoom.RoomImage;
                    item.RoomType.RoomTypeNo = setRoom.RoomType.RoomTypeNo;
                }
                return myoffice.SaveChanges() > 0;//更新到数据库，更新成功，返回true，否则返回false

            } 
        }
        #endregion


        #region 删除会议室信息
		
       /// <summary>
       /// 删除会议室信息
       /// </summary>
       /// <param name="roomNo"></param>
       /// <returns></returns>
            //public bool DeleteMeetingInfor(int roomNo)
            //{
            //    using(MyOfficeEntities myoffice=new MyOfficeEntities ())
            //    {
            //        var result=from c in myoffice.MeetingRooms
            //                   where  c.RoomNo=roomNo
            //                   select c;
            //        foreach (var item in result)
            //       {
            //            myoffice.MeetingRooms.DeleteObject(item);
            //       }
            //        return myoffice .SaveChanges()>0;
            //    }
            //}
 
	       #endregion


        #region 查询会议室类型信息
		    /// <summary>
       /// 查询会议室类型信息
       /// </summary>
       /// <returns></returns>
       public List<RoomType> GetRoomType()
       {
           using(MyOfficeEntities myoffice=new MyOfficeEntities ())
           {
               var type=from c in myoffice.RoomTypes
                        select c;
               return type.ToList();
           }
        }
   
	#endregion


        #region 查询会议室预订信息

       /// <summary>
       /// 查询会议室预订信息
       /// </summary>
       /// <returns></returns>
       public System.Data.DataSet GetRoombook(DateTime time)
       {
           SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyOffice;Integrated Security=True");
           SqlCommand cmd=new SqlCommand();
           cmd.CommandText="EXEC dbo.sp_selectRoomBook @time";
           cmd.Parameters.Add("@time", time);
           cmd.Connection = conn;
           SqlDataAdapter dap = new SqlDataAdapter(cmd);
           System.Data.DataSet ds = new DataSet();
           dap.Fill(ds);
           return ds;
       }
       #endregion
    }
}
